const app = getApp()
// pages/my/index.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		isAuthorize:true, //是否已经授权登录
		userInfo:{}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let that = this;
		//检查用户是否授权登录
		wx.getSetting({
			success(res) {
				//没有授权
				if (!res.authSetting['scope.userInfo']) {
					that.setData({
						isAuthorize:false
					});
					
				}else{
					that.setData({
						isAuthorize: true
					});

				}
			}
		})

		// app.getUserInfo(function (userInfo) {
		// 	//更新数据
		// 	that.setData({
		// 		userInfo: userInfo.data
		// 	})
		// 	console.log(that.data.userInfo);
		// })
	
		wx.getStorage({
			key: 'userInfo',
			success: function (res) {

			that.setData({
				userInfo: res.data
			})
			
			}
		});
	},
	getuserinfo:function(options){
		let that = this;
		that.setData({
			isAuthorize: true
		});
		app.getUserInfo(function (userInfo) {
			//更新数据
			that.setData({
				userInfo: userInfo.data
			})
		})
	},
	wxLogin:function(func) {
		//调用登录接口
		//1.小程序调用wx.login得到code.
		wx.login({
			success: function (res) {
				var code = res['code'];
				//2.小程序调用wx.getUserInfo得到rawData, signatrue, encryptData.
				wx.getUserInfo({
					success: function (info) {
						console.log(info);
						var rawData = info['rawData'];
						var signature = info['signature'];
						var encryptData = info['encryptData'];
						var encryptedData = info['encryptedData']; 
						var iv = info['iv'];

						//3.小程序调用server获取token接口, 传入code, rawData, signature, encryptData.
						wx.request({
							url: constants.LOGIN_URL,
							data: {
								"code": code,
								"rawData": rawData,
								"signature": signature,
								"encryptData": encryptData,
								'iv': iv,
								'encryptedData': encryptedData
							},
							success: function (res) {
								if (res.statusCode != 200) {
									wx.showModal({
										title: '登录失败'
									});
								}
								typeof func == "function" && func(res.data);
							}
						});
					}
				});

			}
		});


	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})